<?php

namespace app\admin\controller\financial;

use service\JsonService;
use app\common\model as M;

/**
 * 提现模块
 */
class Order extends \app\admin\controller\AuthController
{
	public $model = '\app\common\model\Order';

    use \traits\ShotcutControllerTrait;

    /**
     * 消费订单记录
     * @return [type] [description]
     */
    public function consume ()
    {
        return $this->fetch();
    }

    /**
     * 充值订单记录
     * @return [type] [description]
     */
    public function recharge ()
    {
        return $this->fetch();
    }

    /**
     * 退卡记录
     * @return [type] [description]
     */
    public function refund_card ()
    {
        return $this->fetch();
    }

    /**
     * 退套餐记录
     * @return [type] [description]
     */
    public function refund_package ()
    {
        return $this->fetch();
    }

     /**
     * H5商城预购订单记录
     * @return [type] [description]
     */
    public function prepurchase ()
    {
        return $this->fetch();
    }

    /**
     * 取消
     * @method GET
     * @param  [type] $order_id [description]
     * @return [type]           [description]
     */
    public function cancel ($order_id)
    {
        $this->model::beginTrans();
        try {
            $res = M\Order::cancel($order_id);

            if ( $res == true ) {
                $this->model::commitTrans();
                return JsonService::successful('订单已取消');
            }
        } catch ( \Exception $e ) {
            $this->model::rollbackTrans();
            return JsonService::fail($e->getMessage());
        }
        $this->model::rollbackTrans();
        return JsonService::fail('操作失败');
    }

    /**
     * 确认结算
     * @method POST
     * @return [type] [description]
     */
    public function confirm ()
    {
        $post = $this->request->post();

        $confirm_check_error = $this->_confirm_check($post);
        if ( $confirm_check_error != '' ) return JsonService::fail($confirm_check_error);

        $order_id = $post['order_id'] ?? 0;
        $pay_type = $post['pay_type'] ?? 0;
        $remark   = $post['remark'] ?? '';

        if ( !$order_id || !$pay_type ) return JsonService::fail('缺少核心参数');

        if ( !in_array($pay_type , array_keys(M\Order::$pay_type) ) ) return JsonService::fail('不支持的支付方式');

        $order = M\Order::where('id' , $order_id)->find();
        if ( !$order ) return JsonService::fail('订单不存在');

        $res = M\Order::paySuccess($order->order_no , 'IO' . date('YmdHis') . mt_rand(1000,9999) , $pay_type , $remark);
        if ( $res === true ) return JsonService::successful('操作成功');

        if ( is_string($res) ) return JsonService::fail($res);

        return JsonService::fail('操作失败,请稍候再试');
    }

    /**
     * [继承confirm方法的子类前置检查]
     * @param    array            $post [description]
     * @return   [type]                 [description]
     */
    public function _confirm_check (array $post )
    {
        return '';
    }
} //Class End